package com.hejian.android.a72ggametools.utils;

import android.util.Log;

import com.hejian.android.a72ggametools.bean.ActionComment;
import com.hejian.android.a72ggametools.bean.ActionInfo;
import com.hejian.android.a72ggametools.bean.AdvItem;
import com.hejian.android.a72ggametools.bean.Advert;
import com.hejian.android.a72ggametools.bean.Convert;
import com.hejian.android.a72ggametools.bean.GameComment;
import com.hejian.android.a72ggametools.bean.GameDetails;
import com.hejian.android.a72ggametools.bean.GameLike;
import com.hejian.android.a72ggametools.bean.GiftDetail;
import com.hejian.android.a72ggametools.bean.GiftInfo;
import com.hejian.android.a72ggametools.bean.MoneyGame;
import com.hejian.android.a72ggametools.bean.PrizeDetail;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 用来解析Json字符串的工具类
 * Created by Administrator on 2016/1/9.
 */
public class JsonParse {

    private static final String TAG = "hejiandebug";

    /**
     * 用来解析手游礼包请求数据的类
     *
     * @param jsonStr 手游一包请求过来的JSON字符串
     * @return 返回GiftInfo对象的List集合
     */
    public static List<GiftInfo> parseHandGiftJson(String jsonStr) {
        List<GiftInfo> giftInfosList = new ArrayList<GiftInfo>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String consume = jso.getString("consume");
                String content = jso.getString("content");
                String gameName = jso.getString("gamename");
                String giftType = jso.getString("gifttype");
                String icon = jso.getString("icon");
                String id = jso.getString("id");
                String name = jso.getString("name");
                String remain = jso.getString("remain");
                GiftInfo giftInfo = new GiftInfo(consume, content, gameName, giftType, icon, id, name, remain);
                giftInfosList.add(giftInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return giftInfosList;
    }


    public static List<Advert> parseAdvertJson(String jsonStr) {
        List<Advert> advertList = new ArrayList<Advert>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String bimp = jso.getString("bimg");
                String bname = jso.getString("bname");
                String id = jso.getString("id");
                String px = jso.getString("px");
                String target = jso.getString("target");
                String targetId = jso.getString("target_id");
                Advert advert = new Advert(bimp, bname, id, px, target, targetId);
                advertList.add(advert);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return advertList;
    }


    public static List<MoneyGame> parseMoneyJson(String jsonStr) {
        List<MoneyGame> moneyGameList = new ArrayList<MoneyGame>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String allPrize = jso.getString("all_prize");
                String androidDL = jso.getString("android_dl");
                String countDL = jso.getString("count_dl");
                String dlBackScore = jso.getString("dl_back_jifen");
                String h5Src = jso.getString("h5src");
                String icon = jso.getString("icon");
                String id = jso.getString("id");
                String iosDL = jso.getString("ios_dl");
                String name = jso.getString("name");
                String score = jso.getString("score");
                String size = jso.getString("size");
                MoneyGame moneyGame = new MoneyGame(allPrize, androidDL, countDL, dlBackScore, h5Src, icon, id, iosDL, name, score, size);
                moneyGameList.add(moneyGame);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return moneyGameList;
    }

    public static List<AdvItem> parseAdvListJson(String jsonStr) {
        List<AdvItem> advItemList = new ArrayList<AdvItem>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String aeTime = jso.getString("aetime");
                String aName = jso.getString("aname");
                String asTime = jso.getString("astime");
                String hotPic = jso.getString("hotpic");
                String id = jso.getString("id");
                String shortName = jso.getString("shortname");
                String status = jso.getString("status");
                String totalJoinUser = jso.getString("total_join_user");
                AdvItem advItem = new AdvItem(aeTime, aName, asTime, hotPic, id, shortName, status, totalJoinUser);
                advItemList.add(advItem);

            }
            Log.d(TAG, "parseAdvListJson: " + advItemList.size());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return advItemList;
    }


    public static List<Convert> parseConvertJson(String jsonStr) {
        List<Convert> convertList = new ArrayList<Convert>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String consume = jso.getString("consume");
                String icon = jso.getString("icon");
                String id = jso.getString("id");
                String name = jso.getString("name");
                String remain = jso.getString("remain");
                Convert convert = new Convert(consume, icon, id, name, remain);
                convertList.add(convert);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertList;
    }

    public static List<GameLike> parseGameLikeJson(String jsonStr) {
        List<GameLike> gameLikeList = new ArrayList<GameLike>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String countDl = jso.getString("count_dl");
                String icon = jso.getString("icon");
                String id = jso.getString("id");
                String name = jso.getString("name");
                GameLike gameLike = new GameLike(countDl, icon, id, name);
                gameLikeList.add(gameLike);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return gameLikeList;
    }

    public static GameDetails parseGameItemJson(String jsonStr) {
        GameDetails gameDetails = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONObject jso = jsonObject.getJSONObject("info");
            String androidDL = jso.getString("android_dl");
            String countDL = jso.getString("count_dl");
            String dlBackJifen = jso.getString("dl_back_jifen");
            String gameDesc = jso.getString("game_desc");
            String gameTaskStatus = jso.getString("game_task_state");
            ;
            String icon = jso.getString("icon");
            ;
            String id = jso.getString("id");
            ;
            String name = jso.getString("name");
            ;
            String score = jso.getString("score");
            ;
            String size = jso.getString("size");
            ;
            String snapshot = jso.getString("snapshot");
            ;
            String version = jso.getString("version");
            ;
            gameDetails = new GameDetails(androidDL, countDL, dlBackJifen, gameDesc, gameTaskStatus, icon, id, name, score, size, snapshot, version);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return gameDetails;

    }

    public static List<GameComment> parseGameCommentJson(String jsonStr) {
        List<GameComment> gameCommentList = new ArrayList<GameComment>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0, len = jsonArray.length(); i < len; i++) {
                JSONObject jso = jsonArray.getJSONObject(i);
                String content = jso.getString("content");
                String nickName = jso.getString("nickname");
                String pubdate = jso.getString("pubdate");
                GameComment gameComment = new GameComment(content, nickName, pubdate);
                gameCommentList.add(gameComment);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return gameCommentList;
    }

    public static GiftDetail parseGiftDetailJson(String jsonStr) {
        GiftDetail giftDetail = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONObject jso = jsonObject.getJSONObject("info");
            String consume = jso.getString("consume");
            String content = jso.getString("content");
            String eTime = jso.getString("etime");
            String howget = jso.getString("howget");
            ;
            String icon = jso.getString("icon");
            ;
            String name = jso.getString("name");
            ;
            String remain = jso.getString("remain");
            ;
            String sTime = jso.getString("stime");
            ;
            String total = jso.getString("total");
            ;
            String androidDL = jso.getString("android_dl");
            giftDetail = new GiftDetail(consume, content, eTime, howget, icon, name, remain, sTime, total, androidDL);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return giftDetail;
    }

    public static PrizeDetail parsePrizeDetailJson(String jsonStr) {
        PrizeDetail prizeDetail = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONObject jso = jsonObject.getJSONObject("info");
            String consume = jso.getString("consume");
            String icon = jso.getString("icon");;
            String name = jso.getString("name");;
            String remain = jso.getString("remain");
            prizeDetail = new PrizeDetail(consume,icon,name,remain);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return prizeDetail;
    }

    public static ActionInfo parseActionInfoJson(String jsonStr){
        ActionInfo actionInfo = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONObject jso = jsonObject.getJSONObject("info");
            String aName = jso.getString("aname");
             String pubdate = jso.getString("pubdate");
             String writer = jso.getString("writer");
             String content = jso.getString("content");
            actionInfo = new ActionInfo(aName,pubdate,writer,content);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return actionInfo;
    }

    public static List<ActionComment> parseActionCommentJson(String jsonStr){
        List<ActionComment> list = new ArrayList<ActionComment>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0,len = jsonArray.length();i<len;i++){
                JSONObject jso = jsonArray.getJSONObject(i);
                String nickName = jso.getString("nickname");
                String pubdate = jso.getString("pubdate");
                String floor = jso.getString("floor");
                String content = jso.getString("content");
                ActionComment actionComment = new ActionComment(nickName,pubdate,floor,content);
                list.add(actionComment);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
